package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.annotation.RonghuanetPermission;
import cn.ronghuanet.base.utils.AjaxResult;
import cn.ronghuanet.base.utils.PageList;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的相关文档",description = "部门相关的CRUD功能")
//@CrossOrigin
@RonghuanetPermission(name = "部门管理")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /*
        添加和修改写一个接口
        RestFul请求风格:充分利用请求方式替换路径的里面动词
        http的请求方式:GET,
                    HEAD,
                    POST,
                    PUT,
                    PATCH,
                    DELETE,
                    OPTIONS,
                    TRACE;
        使用上面的请求方式替换路径里面的动词
         @RequestMapping(method = RequestMethod.PUT)  当前这个接口只允许使用PUT来请求

     */
    //@RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
    @RonghuanetPermission(name = "部门的添加或修改")
    public AjaxResult saveOrUpdate(@RequestBody
                                       @ApiParam(value ="id为空为新增数据,id不为空则修改数据")
                                               Department department){
        try {
            if (department.getId()==null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }

    }
    //@RequestMapping("/delete")
    @DeleteMapping("/{id}")
    @RonghuanetPermission(name = "部门的删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败："+e.getMessage());
        }

    }

    //@RequestMapping("/queryById")

    @GetMapping("/{id}")
    @RonghuanetPermission(name = "部门查询单条")
    public Department queryById(@PathVariable("id") Long id){
        try {
            return departmentService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    //@RequestMapping("/queryAll")
    @GetMapping
    @RonghuanetPermission(name = "部门查询全部")
    public List<Department> queryAll(){
        try {
            return departmentService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    @GetMapping("/tree")
    @RonghuanetPermission(name = "部门树形结构")
    public List<Department> queryDeptTree(){
        try {
            return departmentService.queryDeptTree();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    @PostMapping
    @RonghuanetPermission(name = "部门的分页查询")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        try {
            return departmentService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

}
